package lws.com.liwushuo.view.fragment.shouye;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import lws.com.liwushuo.R;
import lws.com.liwushuo.adapter.shouye.SearchSecondAdapter;
import lws.com.liwushuo.bean.shouye.SearchSecondBottomBean;
import lws.com.liwushuo.bean.shouye.SearchSecondTopBean;
import lws.com.liwushuo.server.shouye.SearchServer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by ZQ on 2016/10/17.
 */

public class SearchSecondFragment extends Fragment implements Callback<SearchSecondTopBean> {


    private View view;
    private TextView textView;
    private View mainStickHeader;
    private TextView mainStickHeaderTitle;
    private TextView mainStickHeaderRightBtn;
    private RecyclerView recyclerView;
    private SearchServer server;
    //请求参数
    private String sort = "";
    private int limit = 20;
    private int offset = 0;
    private String keyWord;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        //获取关键词
        keyWord = arguments.getString("keyWord");
        //获取依赖的主activity，然后改顶部搜索栏
        SearchMainActivity activity = (SearchMainActivity) getActivity();
        activity.getBackImg().setVisibility(View.VISIBLE);
        activity.getCancleText().setVisibility(View.GONE);
        //activity.getSearchEditText().setText(keyWord);
        activity.getSearchEditText().setHint(keyWord);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.shouye_search_second_fragment, container, false);
            //初始化控件
            initView(view);
        }
        return view;
    }


    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        //网络请求
        server = new Retrofit.Builder()
                .baseUrl("http://api.liwushuo.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(SearchServer.class);
        //获取顶部数据
        server.getTopBean("", 2, 0, keyWord).enqueue(this);
        //设置滑动监听
        final int bottomY = mainStickHeader.getBottom();
        final int measuredHeight = mainStickHeader.getMeasuredHeight();

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // TODO: 2016/10/19  
//                View viewUnder = recyclerView.findChildViewUnder(mainStickHeader.getMeasuredWidth() / 2, 10);
//                if (viewUnder.getTop()<=bottomY){
//                    mainStickHeader.setTranslationY(viewUnder.getTop()-measuredHeight);
//                }
            }
        });

    }

    //初始化控件
    private void initView(View view) {
        //假的粘性头部
        mainStickHeader = view.findViewById(R.id.shouye_search_second_mainstickHeader);
        mainStickHeaderTitle = ((TextView) view.findViewById(R.id.shouye_search_second_mainstickHeader_title));
        mainStickHeaderRightBtn = ((TextView) view.findViewById(R.id.shouye_search_second_mianstickHeader_rightBtn));
        //获取recyclerView，设置管理器
        recyclerView = ((RecyclerView) view.findViewById(R.id.shouye_search_second_recyclerview));
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        //设置间隔
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.bottom = 15;
                outRect.top = 10;
            }
        });
    }

    //顶部请求的网络数据，这里嵌套请求吧，，暂时没想到什么好的方法
    @Override
    public void onResponse(Call<SearchSecondTopBean> call, Response<SearchSecondTopBean> response) {
        final SearchSecondTopBean.DataBean topData = response.body().getData();
        mainStickHeaderTitle.setText("相关攻略(" + topData.getTotal() + ")");
        /**
         * 绝望的嵌套请求，感觉整个人都要崩溃了，
         * 貌似用rxjava更好
         */
        server.getBottomBean(sort, limit, offset, keyWord)
                .enqueue(new Callback<SearchSecondBottomBean>() {
                    @Override
                    public void onResponse(Call<SearchSecondBottomBean> call, Response<SearchSecondBottomBean> response) {
                        SearchSecondBottomBean.DataBean bottomData = response.body().getData();
                        SearchSecondAdapter adapter = new SearchSecondAdapter(getContext(), topData, bottomData);
                        recyclerView.setAdapter(adapter);
                    }

                    @Override
                    public void onFailure(Call<SearchSecondBottomBean> call, Throwable t) {
                    }
                });
    }

    @Override
    public void onFailure(Call<SearchSecondTopBean> call, Throwable t) {

    }
}
